package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

// LoginController 登陆控制器
type LoginController struct {
	beego.Controller
}

// Get 登陆控制器的get请求处理
func (loginCtl *LoginController) Get() {
	if loginCtl.Input().Get("exit") == "true" {
		loginCtl.Ctx.SetCookie("uname", "", -1, "/")
		loginCtl.Ctx.SetCookie("pwd", "", -1, "/")
		loginCtl.Redirect("/", 301)
		return
	}
	loginCtl.TplName = "login.html"
}

// Post 登陆控制器的post请求处理
func (loginCtl *LoginController) Post() {
	input := loginCtl.Input()
	uname := input.Get("uname")
	pwd := input.Get("pwd")
	autoLogin := input.Get("autoLogin") == "on"
	if beego.AppConfig.String("uname") == uname && beego.AppConfig.String("pwd") == pwd {
		maxAge := 0
		if autoLogin {
			maxAge = 1<<31 - 1
		}

		loginCtl.Ctx.SetCookie("uname", uname, maxAge, "/")
		loginCtl.Ctx.SetCookie("pwd", pwd, maxAge, "/")
	}

	loginCtl.Redirect("/", 301)
	return
}

func checkAccount(ctx *context.Context) bool {
	return beego.AppConfig.String("uname") == ctx.GetCookie("uname") && beego.AppConfig.String("pwd") == ctx.GetCookie("pwd")
}
